#-*- coding:utf-8 -*-
import hashlib   
import os
import io
import sys
import re
import time
from shutil import copyfile

md5ListFileName = 'md5list.txt'

def printUsage():
	print ('''Usage: [python] pymd5sum.py ''')

def geneMd5(filename):
    return 'fuckMd5'
    m = hashlib.md5()
    file = io.FileIO(filename,'r')
    bytes = file.read(1024)
    while(bytes != b''):
        m.update(bytes)
        bytes = file.read(1024) 
    file.close()
    return m.hexdigest()
def main():
    if(len(sys.argv) != 1 and len(sys.argv) != 2):
        printUsage()
    mat = "{:48}\t{:32}"
    if(len(sys.argv)==1):
        path="./"
    if(len(sys.argv)==2):
        path=sys.argv[1]
    if(os.path.exists(md5ListFileName)):
        copyfile(md5ListFileName,md5ListFileName.replace('.txt','.'+time.strftime("%Y-%m-%d %H%M%S", time.localtime())+'.txt'))
    with open(md5ListFileName,"w") as md5File:
        for fpathe,dirs,fs in os.walk(path):
            for f in fs:
                full_file = os.path.join(fpathe,f)
                if full_file != './md5list.txt' and re.match('./fileListBack',full_file) == None:
                    md5 = geneMd5(full_file)
                    md5File.write(full_file + "|"+md5 +"\n")
                    print ('update' + ' ' + mat.format(full_file,md5))
            for d in dirs:
                full_file = os.path.join(fpathe,d)
                md5File.write(full_file + "\\" +"\n")
                print ('update' + ' ' + mat.format(full_file,'\\'))
                    
main()